//
//  YLLoginViewController.m
//  YLFinanceService
//
//  Created by she1323 on 2017/8/16.
//  Copyright © 2017年 she1323. All rights reserved.
//  登录界面

#import "YLLoginViewController.h"
#import "YLLoginUserViewModel.h"
#import "YLGlobeHeader.h"
#import <YYText.h>
#import <Masonry.h>
#import "YLMineViewModel.h"
#import "YLRegisterViewController.h"
#import "YLVerificationCodeLoginController.h"
#import "YLForgetPasswordViewController.h"
#import "YLSwitchRootController.h"
#import "WHProgressHUD.h"
#import "WHNetworkTool.h"
#import "YLPushViewModel.h"
@interface YLLoginViewController ()

@property (weak, nonatomic) IBOutlet UITextField *phoneNumberTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
@property (weak, nonatomic) IBOutlet UIButton *userLoginButton;
@property (weak, nonatomic) IBOutlet UIView *phoneNumerLine;
@property (weak, nonatomic) IBOutlet UIView *passwordLine;

@property (nonatomic, strong) YLLoginUserViewModel *viewModel;

@end

@implementation YLLoginViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
  //  [[self.navigationController.navigationBar.subviews objectAtIndex:0]setAlpha:0] ;
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
 //   [[self.navigationController.navigationBar.subviews objectAtIndex:0]setAlpha:0] ;
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    
    
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    [[WHNetworkTool shareInstance] cancel];
    [WHProgressHUD dismiss];
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navBackground"] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:nil];
}


- (YLLoginUserViewModel *)viewModel
{
    if (!_viewModel) {
        _viewModel = [[YLLoginUserViewModel alloc] init];
    }
    return _viewModel;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationController.navigationBar.barStyle = UIStatusBarStyleLightContent;
    
    [self setUpUI];

    [self bindViewModel];
    
    [self loginEvent];
}

- (void)setUpUI
{
    __weak typeof(self) weakSelf = self;
    
    _passwordLine.backgroundColor = YLLineColor;
    _phoneNumerLine.backgroundColor = YLLineColor;
    self.title = @"密码登录";
    
    YYLabel *titleLabel = [YYLabel new];
    [self.view addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(weakSelf.view);
        make.top.mas_equalTo(weakSelf.userLoginButton.mas_bottom).mas_offset(10);
    }];
    titleLabel.attributedText = [self.viewModel attributedStringWithString:@"验证码登录  |  没有账号, 立即注册"];
    
    [[_phoneNumberTextField rac_signalForControlEvents:UIControlEventEditingDidBegin] subscribeNext:^(__kindof UIControl * _Nullable x) {
        weakSelf.phoneNumerLine.backgroundColor = YLMainColor;
        weakSelf.passwordLine.backgroundColor = YLLineColor;
    }];
    
    [[_passwordTextField rac_signalForControlEvents:UIControlEventEditingDidBegin] subscribeNext:^(__kindof UIControl * _Nullable x) {
        weakSelf.phoneNumerLine.backgroundColor = YLLineColor;
        weakSelf.passwordLine.backgroundColor = YLMainColor;
    }];
    
    [_phoneNumberTextField.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
        if (x.length > 11) {
            weakSelf.phoneNumberTextField.text = [weakSelf.phoneNumberTextField.text substringToIndex:11];
        }
    }];
    
    [_passwordTextField.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
        if (x.length > 20) {
            weakSelf.passwordTextField.text = [weakSelf.passwordTextField.text substringToIndex:20];
        }
    }];
    
}

- (void)bindViewModel
{
    RAC(self.viewModel, account) = _phoneNumberTextField.rac_textSignal;
    RAC(self.viewModel, password) = _passwordTextField.rac_textSignal;
}

- (void)setUp
{
    __weak typeof(self) weakSelf = self;
    [[[[YLMineViewModel alloc] init].myInfoCommand execute:nil] subscribeNext:^(id  _Nullable x) {
        [WHProgressHUD dismiss];
        weakSelf.view.userInteractionEnabled = YES;
        [YLSwitchRootController switchRootController];
        [YLSwitchRootController switchMineOrRealName];
        [YLPushViewModel setUpMyPushInfo];
    }];
}

- (void)loginEvent
{
    __weak typeof(self) weakSelf = self;
    RAC(_userLoginButton, enabled) = self.viewModel.loginEnableSiganl;
    
    [[_userLoginButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        
        [[weakSelf.viewModel.loginCommand execute:nil] subscribeNext:^(id  _Nullable x) {
            [weakSelf setUp];
        }];
    }];
    
    [self.viewModel.otherLoginSubject subscribeNext:^(id  _Nullable x) {
        YLVerificationCodeLoginController *vclController = [[YLVerificationCodeLoginController alloc] init];
        [weakSelf.navigationController pushViewController:vclController animated:YES];
    }];
    
    [self.viewModel.registerSubject subscribeNext:^(id  _Nullable x) {
        YLRegisterViewController *registerController = [[YLRegisterViewController alloc] init];
        [weakSelf.navigationController pushViewController:registerController animated:YES];
    }];
}

// 忘记密码
- (IBAction)forgetPassword:(UIButton *)sender {
    YLForgetPasswordViewController *fpController = [[YLForgetPasswordViewController alloc] init];
    [self.navigationController pushViewController:fpController animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
